package com.example.quce.controller;

import com.example.quce.common.R;
import com.example.quce.entity.Quiz1;
import com.example.quce.entity.User;
import com.example.quce.mapper.Quiz1Mapper;
import com.example.quce.mapper.UserMapper;
import com.example.quce.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/quiz1")
@RequiredArgsConstructor
public class Quiz1Controller {

    @Autowired
    Quiz1Mapper quiz1Mapper;
    @Autowired
    UserMapper userMapper;


    private final IUserService userService;
    //private final IQuizService quizService;


    // 读取测试结果
    @GetMapping("/selectByUserId/{userId}")
    public R selectByUserId(@PathVariable int userId) {
        User user = this.userMapper.selectUserById(userId);
        if (user == null) {
            return R.error("用户不存在");
        }
        // 用户存在
        List<Quiz1> list = this.quiz1Mapper.selectByUserId(userId);
        return R.ok(list);
    }

    // 保存测试结果
    @PostMapping("/insertQuiz")
    public R insertQuiz(@RequestBody Quiz1 quiz1) {
//        User user = userService.getById(request.getUserId());
//        if (user == null) {
//            return R.error("用户不存在");
//        }
        int result=this.quiz1Mapper.insertQuiz(quiz1);
        if (result<=0){
            return R.error("用户不存在");
        }
        return R.ok(result);
    }
}
